package artid.com.arttogoa.adapter;

import java.util.List;
import org.apache.commons.lang3.StringUtils;
import artid.com.arttogoa.BindingIbeacon_Activity;
import artid.com.arttogoa.IbeaconStatistics_Activity;
import artid.com.arttogoa.R;
import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.MyArtPush_IbactivityListBean;
import artid.com.arttogoa.parser.OneStateParser;
import artid.com.arttogoa.util.NetUtils;
import artid.com.arttogoa.util.ToastUtil;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：MyArtPush_Adapter   
 * 类描述   ：我的艺推适配器   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-11-7 下午2:32:35   
 *
 */
public class MyArtPush_Adapter extends BaseAdapter{
	private Context mContext;
	private List<MyArtPush_IbactivityListBean> list;
	private DisplayImageOptions mDisplayImageOptions;
	public MyArtPush_Adapter(Context mContext,
			List<MyArtPush_IbactivityListBean> list) {
		super();
		initImageLoader();
		this.mContext = mContext;
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public void notifyData(List<MyArtPush_IbactivityListBean> list){
		this.list = list;
		notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		View v = convertView;
		if (v == null) {
			holder = new ViewHolder();
			v = LayoutInflater.from(mContext).inflate(R.layout.item_myartpush, null);
			holder.IbeaconTitle=(TextView) v.findViewById(R.id.item_myartpush_IbeaconTitle);
			holder.img=(ImageView) v.findViewById(R.id.item_myartpush_img);
			holder.ibactivitytitle=(TextView) v.findViewById(R.id.item_myartpush_ibactivitytitle);
			holder.addtime=(TextView) v.findViewById(R.id.item_myartpush_addtime);
			holder.openBtn=(CheckBox) v.findViewById(R.id.item_myartpush_openBtn);
			holder.bindingBtn=(TextView) v.findViewById(R.id.item_myartpush_bindingBtn);
			holder.statisticsBtn=(TextView) v.findViewById(R.id.item_myartpush_statisticsBtn);
			holder.lastLL=(LinearLayout) v.findViewById(R.id.item_myartpush_lastLL);
			holder.ibeaconListLL=(LinearLayout) v.findViewById(R.id.item_myartpush_IbeaconListLL);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}

		if(!"空数据".equals(list.get(position).getIbactivityid())){
			holder.ibeaconListLL.setVisibility(View.VISIBLE);
			ImageLoader.getInstance().displayImage(list.get(position).getIbactivityimage()
					+ "_"+ NetUtils.dip2px(mContext, 100)+"x"+NetUtils.dip2px(mContext, 100)+".png", holder.img,mDisplayImageOptions);
			holder.ibactivitytitle.setText(list.get(position).getIbactivitytitle());
			holder.addtime.setText(list.get(position).getAddtime());
			if("0".equals(list.get(position).getStatus())){
				//禁用
				holder.openBtn.setChecked(false);
			}else{
				//启用
				holder.openBtn.setChecked(true);
			}
		}else{
			holder.ibeaconListLL.setVisibility(View.GONE);
		}

		//判断是否为第一条数据
		if(!StringUtils.isEmpty(list.get(position).getIbeacon_title())){
			holder.IbeaconTitle.setText("	"+list.get(position).getIbeacon_title());
			holder.IbeaconTitle.setVisibility(View.VISIBLE);
		}else{
			holder.IbeaconTitle.setVisibility(View.GONE);
		}

		//判断是否为最后一条数据
		if(list.get(position).getIsLastData()==true){
			holder.lastLL.setVisibility(View.VISIBLE);
			if(!"空数据".equals(list.get(position).getIbactivityid())){
				holder.statisticsBtn.setVisibility(View.VISIBLE);
			}else{
				holder.statisticsBtn.setVisibility(View.GONE);
			}
		}else{
			holder.lastLL.setVisibility(View.GONE);
		}

		holder.openBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					//开启
					openOrClose(position, list.get(position).getIbactivityid(),"1" );
				}else{
					//关闭
					openOrClose(position,list.get(position).getIbactivityid(), "2");
				}
			}
		});

		//ibeacon绑定
		holder.bindingBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i=new Intent(mContext,BindingIbeacon_Activity.class);
				i.putExtra("MyArtPush_IbactivityListBean", list.get(position));
				i.putExtra("action", "新建");
				mContext.startActivity(i);
			}
		});

		//ibeacon统计
		holder.statisticsBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i=new Intent(mContext,IbeaconStatistics_Activity.class);
				i.putExtra("MyArtPush_IbactivityListBean", list.get(position));
				mContext.startActivity(i);
			}
		});
		return v;
	}
	class ViewHolder {
		ImageView img;
		TextView IbeaconTitle,ibactivitytitle,addtime,bindingBtn,statisticsBtn;
		CheckBox openBtn;
		LinearLayout lastLL,ibeaconListLL;
	}

	private void initImageLoader(){
		mDisplayImageOptions = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.personal_normal) // 设置图片下载期间显示的图片
		.showImageForEmptyUri(R.drawable.personal_normal) // 设置图片Uri为空或是错误的时候显示的图片
		.showImageOnFail(R.drawable.personal_normal) // 设置图片加载或解码过程中发生错误显示的图片
		.resetViewBeforeLoading(false)  // default 设置图片在加载前是否重置、复位
		.delayBeforeLoading(0)  // 下载前的延迟时间
		.cacheInMemory(false) // default  设置下载的图片是否缓存在内存中
		.cacheOnDisk(true) // default  设置下载的图片是否缓存在SD卡中
		.considerExifParams(false) // default
		.imageScaleType(ImageScaleType.EXACTLY) // default 设置图片以如何的编码方式显示
		.bitmapConfig(Bitmap.Config.RGB_565) // default 设置图片的解码类型
		.build();
	}

	/**
	 * 艺术家绑定ibeacon活动 开启/关闭
	 * type 1为启用，2为禁用
	 */
	private void openOrClose(final int position,String ibactivityid, final String type){
		VolleyBasic.getInstance((Activity)mContext).post(Cache.ibactivityOpen,
				VolleyMap.getInstance().ibactivityOpen(ibactivityid,type),
				OneStateParser.class, new VolleyCallback() {

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				OneStateParser bean=(OneStateParser) vd.getData();
				if("1".equals(bean.getState())){
					//成功  
					//设置Status1是正常 0是禁用
					if("1".equals(type)){
						list.get(position).setStatus("1");
					}else{
						list.get(position).setStatus("0");
					}
					notifyDataSetChanged();
					Log.d("log", list.toString());
				}else{
					ToastUtil.toast(mContext, "操作失败，请重试");
				}
			}

			public void onError(VolleyError volleyError) {
				ToastUtil.toast(mContext, "操作失败，请重试");
			}
		});
	}
}
